package com.gsi.webstore.platform.storefront.internal.order;

import java.util.ArrayList;
import java.util.Collection;

import com.gsi.webstore.platform.storefront.capi.order.GSIExtraChargeInformationBO;
import com.gsi.webstore.platform.storefront.capi.order.GSILineItemCtnrCalculationResultView;
import com.gsi.webstore.platform.storefront.capi.order.GSITaxInformationBO;
import com.gsi.webstore.platform.storefront.capi.order.GSIWarrantyLineItemBO;
import com.intershop.beehive.foundation.quantity.Money;
import com.intershop.sellside.appbase.b2c.capi.calculation.LineItemCtnrCalculationResultView;
import com.intershop.sellside.appbase.b2c.capi.order.ProductLineItemBO;
import com.intershop.sellside.appbase.b2c.capi.order.WarrantyLineItemBO;
import com.intershop.sellside.appbase.b2c.capi.order.WarrantyLineItemBOProxy;

/**
 * This is the GSI specific implementation of a warranty line item.
 * 
 * @see WarrantyLineItemBO
 * @author Torsten Herrmann
 *
 */
public class GSIWarrantyLineItemBOImpl extends WarrantyLineItemBOProxy implements GSIWarrantyLineItemBO
{

    private Collection<GSITaxInformationBO> itemPriceBasedTaxes;
    private Collection<GSITaxInformationBO> duties;
    private Collection<GSIExtraChargeInformationBO> fees;
    private Collection<GSIExtraChargeInformationBO> geographicalFees;
    private Collection<GSIExtraChargeInformationBO> importFees;
    
    private Money dutyAmount;
    private ProductLineItemBO delegate;
    private GSILineItemCtnrCalculationResultView calculationResultView;

    /**
     * Constructs a GSI warranty line item.
     * 
     * @param aWarrantyLineItem a warranty line item
     * @param aResultView a calculation result view of a line item container
     */
    public GSIWarrantyLineItemBOImpl(WarrantyLineItemBO aWarrantyLineItem, LineItemCtnrCalculationResultView aResultView)
    {
        super(aWarrantyLineItem);
        
        this.calculationResultView=(GSILineItemCtnrCalculationResultView) aResultView;
    }

    @Override
    public Collection<GSITaxInformationBO> getItemPriceBasedTaxes()
    {
        if (itemPriceBasedTaxes != null)
        {
            return itemPriceBasedTaxes;
        }
        return itemPriceBasedTaxes = ((GSILineItemCtnrCalculationResultView)calculationResultView).getItemPriceBasedTaxes(delegate.getComputedItemID());
    }

    @Override
    public Collection<GSITaxInformationBO> getDuties()
    {
        if (duties == null)
        {
            duties = calculationResultView.getDuties(delegate.getComputedItemID());
        }
        return duties;
    }
    
    @Override
    public Collection<GSIExtraChargeInformationBO> getFees()
    {
        if (fees == null)
        {
            fees = new ArrayList<GSIExtraChargeInformationBO>(); 
            fees.addAll(getGeographicalFees());
            fees.addAll(getImportFees());
        }
        return fees;
    }

    @Override
    public Collection<GSIExtraChargeInformationBO> getGeographicalFees()
    {
        if (geographicalFees == null)
        {
            geographicalFees = calculationResultView.getGeographicalFees(delegate.getComputedItemID());
        }
        return geographicalFees;
    }

    @Override
    public Collection<GSIExtraChargeInformationBO> getImportFees()
    {
        if (importFees == null)
        {
            importFees = calculationResultView.getImportFees(delegate.getComputedItemID());
        }
        return importFees;
    }

    @Override
    public Money getDutyAmount()
    {
        if (dutyAmount == null)
        {
            dutyAmount = calculationResultView.getDutyAmount(delegate.getComputedItemID());
        }
        return dutyAmount;
    }

    @Override
    public Money getNetBasePrice()
    {
        return ((GSILineItemCtnrCalculationResultView)calculationResultView).getWarrantyLineItemNetTotal(delegate.getComputedItemID());
    }

}
